package besta.moon.engine.selector.result;

import besta.moon.engine.RowSource;
import besta.moon.sql.expression.Expressions;

public final class NoFromResult extends RowSource {

    private int rowPos;

    @Override
    public final boolean isScrollable() {
        return true;
    }

    @Override
    public final void beforeFirst() {
        rowPos = 0;
    }

    @Override
    public final boolean isBeforeFirst() {
        return rowPos <= 0;
    }

    @Override
    public final boolean isFirst() {
        return rowPos == 1;
    }

    @Override
    public final boolean first() {
        rowPos = 1;
        return true;
    }

    @Override
    public final boolean previous() {
        rowPos--;
        return rowPos == 1;
    }

    @Override
    public final boolean next() {
        rowPos++;
        return rowPos == 1;
    }

    @Override
    public final boolean last() {
        rowPos = 1;
        return true;
    }

    @Override
    public final boolean isLast() {
        return rowPos == 1;
    }

    @Override
    public final boolean isAfterLast() {
        return rowPos > 1;
    }

    @Override
    public final void afterLast() {
        rowPos = 2;
    }

    @Override
    public final boolean absolute(int row) {
        rowPos = (row > 0) ? Math.min(row, 1) : Math.min(row + 1, -1);
        return rowPos == 1;
    }

    @Override
    public final boolean relative(int rows) {
        if (rows == 0) {
            return rowPos == 1;
        }
        rowPos = Math.min(Math.max(rowPos + rows, -1), 1);
        return rowPos == 1;
    }

    @Override
    public final int getRow() {
        return rowPos == 1 ? 1 : 0;
    }

    @Override
    public final long getRowPosition() {
        return rowPos;
    }

    @Override
    public final void setRowPosition(long rowPosition) {
        rowPos = (int) rowPosition;
    }

    @Override
    public final boolean rowInserted() {
        return false;
    }

    @Override
    public final boolean rowDeleted() {
        return false;
    }

    @Override
    public final void nullRow() {
        throw new Error();
    }

    @Override
    public final void noRow() {
        throw new Error();
    }

    @Override
    public final void execute() throws Exception {
    }

    @Override
    public boolean isExpressionsFromThisRowSource(Expressions columns) {
        return columns.size() == 0;
    }
}
